package com.castle.fortress.admin.member.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.dto.MemberPointsDto;
import com.castle.fortress.admin.member.entity.MemberPointsEntity;
import com.castle.fortress.admin.member.mapper.MemberPointsMapper;
import com.castle.fortress.admin.member.service.MemberPointsService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员积分表 服务实现类
 *
 * @author Mgg
 * @since 2021-11-27
 */
@Service
public class MemberPointsServiceImpl extends ServiceImpl<MemberPointsMapper, MemberPointsEntity> implements MemberPointsService {
    /**
    * 获取查询条件
    * @param memberPointsDto
    * @return
    */
    public QueryWrapper<MemberPointsEntity> getWrapper(MemberPointsDto memberPointsDto){
        QueryWrapper<MemberPointsEntity> wrapper= new QueryWrapper();
        if(memberPointsDto != null){
            MemberPointsEntity memberPointsEntity = ConvertUtil.transformObj(memberPointsDto,MemberPointsEntity.class);
            wrapper.like(memberPointsEntity.getId() != null,"id",memberPointsEntity.getId());
            wrapper.like(memberPointsEntity.getMemberId() != null,"member_id",memberPointsEntity.getMemberId());
            wrapper.like(StrUtil.isNotEmpty(memberPointsEntity.getBalance()),"balance",memberPointsEntity.getBalance());
            wrapper.like(StrUtil.isNotEmpty(memberPointsEntity.getFrozen()),"frozen",memberPointsEntity.getFrozen());
            wrapper.like(memberPointsEntity.getStatus() != null,"status",memberPointsEntity.getStatus());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberPointsDto> pageMemberPoints(Page<MemberPointsDto> page, MemberPointsDto memberPointsDto) {
        QueryWrapper<MemberPointsEntity> wrapper = getWrapper(memberPointsDto);
        Page<MemberPointsEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberPointsEntity> memberPointsPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberPointsDto> pageDto = new Page(memberPointsPage.getCurrent(), memberPointsPage.getSize(),memberPointsPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberPointsPage.getRecords(),MemberPointsDto.class));
        return pageDto;
    }


    @Override
    public List<MemberPointsDto> listMemberPoints(MemberPointsDto memberPointsDto){
        QueryWrapper<MemberPointsEntity> wrapper = getWrapper(memberPointsDto);
        List<MemberPointsEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberPointsDto.class);
    }
}

